package jianzhiOffer;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Num32_1 {
    public int[] levelOrder(TreeNode root) {
        if(root == null) {
            return new int[0];
        }
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if(node.left != null) queue.offer(node.left);
            if(node.right != null) queue.offer(node.right);
            res.add(node.val);
        }
        int len = res.size();
        int[] arr = new int[len];
        int index = 0;
        for(int i : res) {
            arr[index] = i;
            index++;
        }
        return arr;
    }
}
